﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
using lib;

namespace DocClient
{
    public partial class Grafieken : Form
    {
        private int count = 0;
        private lib.TestResult result;
        private DoctorForm docform;
        private DataTable table = new DataTable();
        bool once = true;

        public Grafieken(string results, DoctorForm docform)
        {
            InitializeComponent();
            this.docform = docform;
            result = new lib.TestResult(results);
            changeLabel(true);
            GrafiekManager.getInstance().registerGrafiek(this);
        }

        public Grafieken(TestResult results, DoctorForm docform)
        {
            InitializeComponent();
            this.docform = docform;
            result = results;
            changeLabel(true);
            GrafiekManager.getInstance().registerGrafiek(this);
        }

        private void previous_Click(object sender, EventArgs e)
        {
            changeLabel(false);
        }

        private void next_Click(object sender, EventArgs e)
        {
            changeLabel(true);
        }

        private void changeLabel(Boolean add)
        {
            if (count < 5 && add == true)
            {
                count++;
                load(count);
                label1.Text = count + "/" + "5";
            }
            if (count > 1 && add == false)
            {
                count--;
                load(count);
                label1.Text = count + "/" + "5";
            }
        }

        private void createTable()
        {
            dataGridView1.DataSource = table;
            table.Columns.Add("Tijd", typeof(int));
            table.Columns.Add("Speed", typeof(float));
            table.Columns.Add("HearthBeat", typeof(int));
            table.Columns.Add("Distance", typeof(int));
            table.Columns.Add("Energy", typeof(int));
            table.Columns.Add("RPM", typeof(int));
            table.Columns.Add("Current Power", typeof(int));
            int tijd = 1;
            foreach (lib.MeasuredValues measurement in result.GetMeasuredValues())
            {
                table.Rows.Add(tijd, (measurement.speed / 10), measurement.heartBeat, measurement.distance, measurement.energy, measurement.rpm, measurement.power);
                tijd++;
            }

        }

        private void load(int graphnummer)
        {
            if (once)
            {
                createTable();
                once = false;
            }
            chart1.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
            if (graphnummer == 1)
	        {
                String naam = "Speed (KM/H)";
                chart1.Series.Clear();
                chart1.Series.Add(naam);
                chart1.Series[naam].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series[naam].Color = Color.Red;
                int i = 0;
                    foreach (lib.MeasuredValues measurement in result.GetMeasuredValues())
                    {
                        chart1.Series[naam].Points.AddXY(i, (float)(measurement.speed/10));
                        i++;
                    }
            }

            if (graphnummer == 2)
            {
                String naam = "HearthBeat (BPM)";
                chart1.Series.Clear();
                chart1.Series.Add(naam);
                chart1.Series[naam].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series[naam].Color = Color.Blue;
                int i = 0;
                foreach (lib.MeasuredValues measurement in result.GetMeasuredValues())
                {
                    chart1.Series[naam].Points.AddXY(i, measurement.heartBeat);
                    i++;
                }
            }

            if (graphnummer == 3)
            {
                String naam = "Distance (KM)";
                chart1.Series.Clear();
                chart1.Series.Add(naam);
                chart1.Series[naam].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series[naam].Color = Color.Green;
                int i = 0;
                foreach (lib.MeasuredValues measurement in result.GetMeasuredValues())
                {
                    chart1.Series[naam].Points.AddXY(i, measurement.distance);
                    i++;
                }
            }

            if (graphnummer == 4)
            {
                String naam = "Energy (KJ)";
                chart1.Series.Clear();
                chart1.Series.Add(naam);
                chart1.Series[naam].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series[naam].Color = Color.BlueViolet;
                int i = 0;
                foreach (lib.MeasuredValues measurement in result.GetMeasuredValues())
                {
                    chart1.Series[naam].Points.AddXY(i, measurement.energy);
                    i++;
                }
            }

            if (graphnummer == 5)
            {
                String naam = "RPM";
                chart1.Series.Clear();
                chart1.Series.Add(naam);
                chart1.Series[naam].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Line;
                chart1.Series[naam].Color = Color.Orange;
                int i = 0;
                foreach (lib.MeasuredValues measurement in result.GetMeasuredValues())
                {
                    chart1.Series[naam].Points.AddXY(i, measurement.rpm);
                    i++;
                }
            }


    
        }
    }
}
